package cn.itcast.dao.impl;

import cn.itcast.dao.ReqDao;
import cn.itcast.domain.Req;
import cn.itcast.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class ReqDaoImpl implements ReqDao {
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Req> findAll() {
        String sql="select * from req";
        List<Req> reqs=null;
        try {
            reqs=template.query(sql,new BeanPropertyRowMapper<Req>(Req.class));
            return reqs;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Req> findReqsByYid(int parseInt) {
        String sql="select * from req where yid=?";
        List<Req> reqs=null;
        try {
            reqs=template.query(sql,new BeanPropertyRowMapper<Req>(Req.class),parseInt);
            return reqs;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Req findReqByIdAndYid(int parseInt, int parseInt1) {
        String sql="select * from req where id=? and yid=?";
        Req req=null;
        try {
            req=template.queryForObject(sql,new BeanPropertyRowMapper<Req>(Req.class),parseInt,parseInt1);
            return req;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void updateReqInfo(int parseInt, int parseInt1, String info) {
        String sql="update req set info=?,state_id=?,change_state=?,check_state=? where id=? and yid=?";
        try {
            template.update(sql,info,1,0,0,parseInt,parseInt1);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateReqStateId(int id, int state_id) {
        String sql="update req set state_id=? ,change_state=0,check_state=0 where id=?";
        try {
            template.update(sql,state_id,id);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateReqChangeState(int id, int change_state) {
        String sql="update req set change_state=? ,check_state=0 where id = ?";
        try {
            template.update(sql,change_state,id);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateReqCheckState(int id, int check_state) {
        String sql="update req set check_state=? where id=?";
        try {
            template.update(sql,check_state,id);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addReq(Req req) {
        String sql="insert into req values(?,0,?,1,?,0,0)";
        try {
            template.update(sql,req.getId(),req.getInfo(),req.getYid());
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }


}
